home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / snip9_91.arc / A2B.C < prev    next >
C/C++ Source or Header  |  1991-09-17  |  4KB  |  176 lines

  1. /* a2b.c via atob: version 4.0
  2.  * stream filter to change printable ascii from "btoa" back into 8 bit bytes
  3.  * if bad chars, or Csums do not match: exit(1) [and NO output]
  4.  *
  5.  *  Paul Rutter     Joe Orost      DosPort WildHack
  6.  *  philabs!per     petsd!joe      Severely!Mangled
  7.  */
  8.  
  9. #include <stdio.h>
  10.  
  11. #include "ext_refs.c"
  12.  
  13. #define reg register
  14.  
  15. #define streq(s0, s1)  (strcmp(s0, s1) == 0)
  16.  
  17. #define times85(x)  ((((((x<<2)+x)<<2)+x)<<2)+x)
  18.  
  19. void fatal(void);
  20. void decode(reg c);
  21. void byteout(reg c);
  22.  
  23. long int Ceor = 0;
  24. long int Csum = 0;
  25. long int Crot = 0;
  26. long int word = 0;
  27. long int bcount = 0;
  28.  
  29. void fatal() {
  30.     error(1, __FILE__, __LINE__, "bad format or Csum to atob\n");
  31. }
  32.  
  33. #define DE(c) ((c) - '!')
  34.  
  35. void decode(reg c)
  36. {
  37.   if (c == 'z') {
  38.     if (bcount != 0) {
  39.       fatal();
  40.     } else {
  41.       byteout(0);
  42.       byteout(0);
  43.       byteout(0);
  44.       byteout(0);
  45.     }
  46.   } else if ((c >= '!') && (c < ('!' + 85))) {
  47.     if (bcount == 0) {
  48.       word = DE(c);
  49.       ++bcount;
  50.     } else if (bcount < 4) {
  51.       word = times85(word);
  52.       word += DE(c);
  53.       ++bcount;
  54.     } else {
  55.       word = times85(word) + DE(c);
  56.       byteout((int)((word >> 24) & 255));
  57.       byteout((int)((word >> 16) & 255));
  58.       byteout((int)((word >> 8) & 255));
  59.       byteout((int)(word & 255));
  60.       word = 0;
  61.       bcount = 0;
  62.     }
  63.   } else {
  64.     fatal();
  65.   }
  66. }
  67.  
  68. FILE *tmp_file;
  69.  
  70. void byteout(reg c)
  71. {
  72.   Ceor ^= c;
  73.   Csum += c;
  74.   Csum += 1;
  75.   if ((Crot & 0x80000000L)) {
  76.     Crot <<= 1;
  77.     Crot += 1;
  78.   } else {
  79.     Crot <<= 1;
  80.   }
  81.   Crot += c;
  82.   putc(c, tmp_file);
  83. }
  84.  
  85. main(int argc, char *argv[])
  86. {
  87.   reg c;
  88.   reg long int i;
  89.   char tmp_name[100];
  90.   char buf[100];
  91.   long int n1, n2, oeor, osum, orot;
  92.   int fs_h_si=0, fs_h_so=0;
  93.  
  94. #if 0
  95.   if (argc != 1) 
  96.     error(2, __FILE__, __LINE__, "bad args to %s\n", argv[0]);
  97.   sprintf(tmp_name, "/usr/tmp/atob.%x", getpid());
  98. #else
  99.   /* Filter function suppressed until binary I/O is provided */
  100.   switch (argc) {
  101.   default:
  102.     error(1, __FILE__, __LINE__, "bad args to %s\n"
  103.     "Usage:\nA2B  [InFile.B2A]  OutFile.Bin\n"
  104.     "A2B        : converts ASCII encoded files back to binary format\n"
  105.     "InFile.B2A : name of existing, ASCII encoded binary, file to convert\n"
  106.     "OutFile.Bin: name for the file to get the decoded binary format\n"
  107.     , argv[0]);
  108.     break;
  109.   case 3:
  110.     fs_h_si=fs_chng(stdin , argv[1], "r"   );
  111.   /*break;*/
  112.   case 2:
  113.     fs_h_so=fs_chng(stdout, argv[argc-1], OPN_WR);
  114.     if (fs_h_si < 0 || fs_h_so < 0) 
  115.         error(3, __FILE__, __LINE__, "bad args to %s\n", argv[0]);
  116.     break;
  117.   }
  118.   tmpnam(tmp_name);
  119. #endif
  120.   tmp_file = fopen(tmp_name, OPN_W_R);
  121.   if (tmp_file == NULL) {
  122.     fatal();
  123.   }
  124. #if 0
  125.   /* HERE it will make clusters disappear from the drive */
  126.   unlink(tmp_name); /* Make file disappear */
  127. #endif
  128.   /*search for header line*/
  129.   for (;;) {
  130.     if (fgets(buf, sizeof buf, stdin) == NULL) {
  131.       fatal();
  132.     }
  133.     if (streq(buf, "xbtoa Begin\n")) {
  134.       break;
  135.     }
  136.   }
  137.  
  138.   while ((c = getchar()) != EOF) {
  139.     if (c == '\n') {
  140.       continue;
  141.     } else if (c == 'x') {
  142.       break;
  143.     } else {
  144.       decode(c);
  145.     }
  146.   }
  147.   if(scanf("btoa End N %ld %lx E %lx S %lx R %lx\n",
  148.          &n1, &n2, &oeor, &osum, &orot) != 5) {
  149.     fatal();
  150.   }
  151.   if ((n1 != n2) || (oeor != Ceor) || (osum != Csum) || (orot != Crot)) {
  152.     fatal();
  153.   } else {
  154.     /*copy OK tmp file to stdout*/;
  155.     fseek(tmp_file, 0L, 0);
  156.     for (i = n1; --i >= 0;) {
  157.       putchar(getc(tmp_file));
  158.     }
  159.   }
  160.   fclose(tmp_file);  /* unlink()ing it while it was open dropped clusters */
  161.   unlink(tmp_name);  /* HERE the temp file is not needed */
  162.   switch (argc) {
  163.   case 3:
  164.     fs_h_si=fs_rest( stdin , fs_h_si); 
  165.   /*break;*/
  166.   case 2:
  167.     fs_h_so=fs_rest( stdout, fs_h_so);
  168.     if (fs_h_si < 0 || fs_h_so < 0) 
  169.         error(4, __FILE__, __LINE__, "bad args to %s\n", argv[0]);
  170.     break;  
  171.   default: break;  
  172.   }
  173.   exit(0);
  174. return 0; /* Silence the warning without void'ing the function */
  175. }
  176.